package com.study.d012Components.treeview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/** TreeView基本使用
*@description
*@author xtayhame
*date 2020年3月15日
*version
*since v1.0
*/
public class TreeViewMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane an = new AnchorPane();
		Button b1 = new Button("点击");

		TreeView<String> treeView = new TreeView<String>();
		treeView.setPrefSize(200, 300);

		TreeItem<String> root = new TreeItem<String>("中国");

		TreeItem<String> item_gd = new TreeItem<String>("广东省");
		TreeItem<String> item_gd_1 = new TreeItem<String>("广州市");
		TreeItem<String> item_gd_2 = new TreeItem<String>("深圳市");
		TreeItem<String> item_gd_3 = new TreeItem<String>("佛山市");
		item_gd.getChildren().addAll(item_gd_1,item_gd_2,item_gd_3);


		TreeItem<String> item_hn = new TreeItem<String>("河南省");
		TreeItem<String> item_hn_1 = new TreeItem<String>("南阳市");
		TreeItem<String> item_hn_2 = new TreeItem<String>("商丘市");
		TreeItem<String> item_hn_3 = new TreeItem<String>("驻马店市");
		item_hn.getChildren().addAll(item_hn_1,item_hn_2,item_hn_3);

		root.getChildren().addAll(item_gd,item_hn);

		treeView.setRoot(root);


		an.getChildren().addAll(b1,treeView);
		AnchorPane.setTopAnchor(treeView, 30.0);
		AnchorPane.setLeftAnchor(treeView, 50.0);
		stage.setScene(new Scene(an, 400, 500));
		stage.show();

		//设置根节点展开
	/*	root.setExpanded(true);

		System.out.println("获取根节点下的子节点个数："+root.getChildren().size());

		System.out.println("root节点是否展开：" + root.isExpanded() + "   是否是叶子节点：" + root.isLeaf());

		System.out.println("广东省父节点值："+item_gd.getParent().getValue());

		System.out.println("获取同级节点的前一个节点："+item_gd_2.previousSibling().getValue());

		System.out.println("获取同级节点的下一个节点："+item_gd_2.nextSibling().getValue());

		System.out.println("获取item_hn_2的下一个节点："+item_hn_1.nextSibling(item_hn_2).toString());*/

		//设置列转换
		treeView.setCellFactory(TextFieldTreeCell.forTreeView(new StringConverter<String>() {
			@Override
			public String toString(String object) {
				// TODO Auto-generated method stub
				return object.toString() + "-|";
			}

			@Override
			public String fromString(String string) {
				// TODO Auto-generated method stub
				return string + "*";
			}
		}));

		//设置单选
		treeView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		//监听鼠标点击的节点
		treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
			@Override
			public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue,
					TreeItem<String> newValue) {
				System.out.println("当前选择的是：" + newValue.getValue());
			}
		});


		//节点合起事件
		//注意：这里要添加在跟节点上，因为事件是从子节点向父节点传递的，如果设置在子节点上，则其他父节点无法收到
		root.addEventHandler(TreeItem.branchCollapsedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {

			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("当前合起的节点是："+event.getSource().getValue());
			}
		});

		//节点展开事件
		root.addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("当前展开的节点是："+event.getSource().getValue());
			}
		});


		//TreeItem事件
		root.addEventHandler(TreeItem.expandedItemCountChangeEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("当前展开或合起的节点是："+event.getSource().getValue());
			}
		});

		root.addEventHandler(TreeItem.valueChangedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("当前修改的的节点是："+event.getSource().getValue());
			}
		});

		root.addEventHandler(TreeItem.graphicChangedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("修改图标-当前修改的的节点是："+event.getSource().getValue());
			}
		});

		//所有事件都会触发TreeItem.treeNotificationEvent()
		root.addEventHandler(TreeItem.treeNotificationEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
			@Override
			public void handle(TreeModificationEvent<String> event) {
				System.out.println("treeNotificationEvent-当前修改的的节点是："+event.getSource().getValue());
			}
		});


		b1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//触发TreeItem.valueChangedEvent() 事件
				item_hn_3.setValue("驻马店市1");

				//触发TreeItem.graphicChangedEvent()
				//item_gd_3.setGraphic(new CheckBox(""));

			}
		});
	}
}
